//
//  KCDuDaoMessageController.swift
//  MyKC
//
//  Created by yy on 2017/2/22.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit
import SVProgressHUD
import YYModel

class KCDuDaoMessageController: UIViewController {
    // MARK: -  懒加载基本控件------
    internal lazy var noticeContentModel = KCHomeNoticeContentModel()
//    var array: Array<KCLessMessageModel> = []
    lazy var lessMessageArray = [KCLessMessageModel]()
    lazy var lessTotalNumStr = String()
    lazy var totalNumStr = String()
    lazy var tableView: UITableView = {
        let tb = UITableView(frame: UIScreen.main.bounds)
        tb.delegate = self
        tb.dataSource = self
        let footView = UIView()
        tb.tableFooterView = footView
        
        //注册nib文件
        tb.register(UINib(nibName: "KCNoticetileCell", bundle: nil), forCellReuseIdentifier: "KCNoticetileCellId")
        tb.register(UINib(nibName: "KCLessMessageCell", bundle: nil), forCellReuseIdentifier: "KCLessMessageCellId")
        return tb
    }()
    var headView: KCSystermNoticeHeadView = {
        let hv = KCSystermNoticeHeadView.systermNoticeHeadView()
        hv.lbe_head.text = "系统公告"
        hv.hideView.isHidden = true
        return hv
    }()
    var headView2: KCSystermNoticeHeadView = {
        let hv = KCSystermNoticeHeadView.systermNoticeHeadView()
        hv.lbe_head.text = "门店缺货提醒"
        hv.hideView.isHidden = false
        return hv
    }()
    
    // MARK: -  设置界面信息-------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableView)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 50
        getData()
    }
    
    // MARK: -  获取网络数据---
    private func getData() {
        SVProgressHUD.show()
        //获取缺货信息的网络请求-------------
        KCNetworkManager.sharedTools.request(method: .GET, urlString: "/api/Task/OutOfStockStore", parameters: nil) { (response, error) in
            SVProgressHUD.dismiss()
            guard let response = response as? NSDictionary,let responseContent = response["Content"] as? NSDictionary,let responseArray = responseContent["StoreOutOfStocks"] as? NSArray, let responseExtra = responseContent["Extra"] as? NSString else {
                return
            }
            self.lessMessageArray = NSArray.yy_modelArray(with: KCLessMessageModel.self, json: responseArray) as! [KCLessMessageModel]
            let extraArray = responseExtra.components(separatedBy: ";")
            self.lessTotalNumStr = extraArray.first ?? ""
            self.totalNumStr = extraArray.last ?? ""
            self.tableView.reloadData()
        }
    }
}

// MARK: -
// MARK: tableView-- 代理方法------
extension KCDuDaoMessageController: UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //每行的个数是根据--返回数据的数量来确定的  第二组返回数量加上1
        return section == 0 ? allNotice.count : lessMessageArray.count
    }
    
    // MARK: -  返回cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = getIdentifier(indexPath: indexPath)
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.selectionStyle = .none
        setCell(cell: cell, indexPath: indexPath)
        return cell
    }

    // MARK: -  cell的点击事件-----
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            SVProgressHUD.show()
            let model = allNotice[indexPath.row]
            let dic = ["AnnouncementId":model.AnnouncementId]
            KCNetworkManager.sharedTools.request(method: .GET, urlString: "/api/Announcement/GetAnnouncement", parameters: dic, finished: { (response, error) in
                SVProgressHUD.dismiss()
                guard let responseObj = response as? NSDictionary,let responseContent = responseObj["Content"] as? NSDictionary else {
                    return
                }
                self.noticeContentModel.yy_modelSet(with: responseContent as! [AnyHashable : Any])
                let notice = KCNoticeDetailController(nibName: "KCNoticeDetailController", bundle: nil)
                notice.noticeContentModel = self.noticeContentModel
                self.present(notice, animated: true, completion: nil)
            })
        }
    }
    
    private func getIdentifier(indexPath: IndexPath) -> String {
        return indexPath.section == 0 ? "KCNoticetileCellId" : "KCLessMessageCellId"
    }
    
    private func setCell(cell: UITableViewCell,indexPath: IndexPath) -> Void {
        cell.selectionStyle = .none
        if indexPath.section == 0 {
            let model = allNotice[indexPath.row]
            let cellNoticeContent = cell as! KCNoticetileCell
            cellNoticeContent.lbe_notice.text = model.Title
        }else {
            let cellLess = cell as! KCLessMessageCell
            cellLess.model = lessMessageArray[indexPath.row]
//            cellLess.lbe_storeName.text = "家乐福上海分店家乐福上"
//            cellLess.lbe_address.text = "家乐福上海分店家乐福上海"
        }
    }
    // MARK: -  返回组头
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        headView.lineView.isHidden = allNotice.count == 0 ? false : true
        headView2.lineView.isHidden = lessMessageArray.count != 0
        headView2.lbe_lessNum.text = self.lessTotalNumStr
        headView2.lbe_totleNum.text = "/ " + self.totalNumStr
        return section == 0 ? headView : headView2
    }
    // MARK: -  返回组头的高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }
}



